package cn.tedu.redisdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class Config {
    @Bean
    public RedisTemplate initRedisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
        //连接工厂还是使用Spring默认提供的即可
        redisTemplate.setConnectionFactory(factory);
        //设置Redis中的Key类型的序列化器，序列化为字符串类型
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
         /*
                key    value
            SET("张三",person)

            class Person{
                private Integer id;
                private String name;
            }

            SET 张三
            {"id": "123","name": "张三"}

         */
        redisTemplate.setHashKeySerializer(RedisSerializer.json());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }
}
